import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { VersionCheckService } from './service/version-check.service';
import { config } from 'src/config';
import { LoginUser } from 'src/kalend';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  providers: [VersionCheckService]
})
export class AppComponent implements OnInit {
  /**
   * 判断是否显示标题栏
   */
  constructor(protected router: Router, private vc: VersionCheckService, private modalService: NzModalService) {
    if (!this.islogined() && window.location.hash.substring(1) !== '/login') {
      router.navigate(['/login']);
    }
  }
  ngOnInit() {
    const foo = () => {
      this.vc.check().then((resp: string) => {
        let bPass = false;
        if (resp === config.version) {
          bPass = true;
        } else if (resp !== '') {
          const a = resp.split('.');
          const b = config.version.split('.');
          bPass = true;
          const smaller = a.some((el, i) => {
            // tslint:disable-next-line:radix
            if (b[i] && parseInt(el) < parseInt(b[i])) { return true; }
          });
          // 先判断 smaller再判断 bigger看上去有些重复，实际是为了防止 例如API给的版本是 0.2.3 本地版本是 0.3.1 的情况。
          if (!smaller) {
            const bigger = a.some((el, i) => {
              // tslint:disable-next-line:radix
              if (b[i] && parseInt(el) > parseInt(b[i])) { return true; }
            });
            if (bigger) { bPass = false; }
          }
        }

        if (bPass) {
          // schedule next check
          setTimeout(foo, 1000 * 60);
        } else {
          this.modalService.warning({
            nzTitle: '提示信息',
            nzContent: `系统版本已更新，10秒后将自动刷新网站`
          });
          // this.msg.warning('系统版本已更新，10秒后将自动刷新网站');
          this.vc.needRefresh = true;
          setTimeout(() => {
            window.location.href = config.outfallMap + '/#/login?a=' + (new Date()).getTime();
          }, 1000 * 10);
          // window.location.reload();
        }
      });
    };
    setTimeout(foo, 5000);
  }

  islogined() {
    return LoginUser.isLogin;
  }

  header(): Boolean {
    return layoutSetting.header;
  }

  /**
   * 判断是否显示菜单栏
   *
   */
  menu(): Boolean {
    return layoutSetting.menu;
  }


  /**
     * 是否显示遮罩
     */
  isSpinning(): Boolean {
    return layoutSetting.isSpinnings;
  }

}
const layoutSetting = {
  header: true,
  menu: true,
  isSpinnings: false
};

export class ComponentBasic {
  /**
   * 设置页面布局中是否显示标题栏或菜单栏
   * @param header  是否显示标题栏
   * @param menu 是否显示菜单栏（如果标题栏设置为不显示'false'，那么菜单栏也会自动设置为不显示出来）
   *
   */
  setLayout(header: boolean = true, menu: boolean = true) {
    layoutSetting.header = header;
    layoutSetting.menu = header && menu;
  }
  /**
    * 设置遮罩时候显示
    */
  setisSpinning(isSpinnings: boolean = false) {
    layoutSetting.isSpinnings = isSpinnings;
  }
  constructor() {
    this.setLayout(layoutSetting.header, layoutSetting.menu);
    this.setisSpinning(layoutSetting.isSpinnings);
  }
  // tslint:disable-next-line:eofline
}